//
//  OCSSAddressSelectView.swift
//  OCSSStoreSwift
//
//  Created by MacMiniKangChe on 2018/11/5.
//  Copyright © 2018年 MacMiniKangChe. All rights reserved.
//

import UIKit
import SnapKit

class OCSSAddressSelectView: UIView,UITableViewDelegate, UITableViewDataSource,UIScrollViewDelegate {

    var provinceLab: UILabel!
    var scrollView: OCSSBaseScrollView!
    
    var areaId:String? = "";
    
    override init(frame: CGRect) {
        super.init(frame: frame);
        initUI();
        requestAddressData();
    }
    func initUI() -> Void {
        self.backgroundColor = UIColor.white;
        //创建标题
        let titleLab = UILabel.init();
        titleLab.textColor = UIColor.black;
        titleLab.text = "选择地址";
        titleLab.font = UIFont.init(name: "PingFangSC-Semibold", size: 16);
        self.addSubview(titleLab);
        titleLab.snp.makeConstraints { (maker) in
            maker.top.equalTo(10);
            maker.centerX.equalTo(self.centerX);
            maker.size.equalTo(CGSize.init(width: 80, height: 15));
        }
        //创建选择按钮
        for i in 0...1 {
            let btn = UIButton.init(type: UIButtonType.custom);
            if i == 0 {
                btn.setTitle("未选择", for: UIControlState.normal);
            }
            btn.tag = i+100;
            btn.titleLabel?.font = UIFont.init(name: "PingFangSC", size: 14);
            btn.setTitleColor(UIColor.black, for: UIControlState.normal);
            btn.frame = CGRect.init(x: 12+(80+10)*i, y: 35, width: 80, height: 30);
            self.addSubview(btn);
        }
        //创建下划线
        let line = UIView.init();
        line.backgroundColor = UIColor.init(hexString: "f7f7f7")
        self.addSubview(line);
        line.snp.makeConstraints { (make) in
            make.top.equalTo(titleLab.bottom).offset(65);
            make.left.equalTo(self.left);
            make.right.equalTo(self.right);
            make.height.equalTo(1);
        }
        //创建scrollView
        scrollView = OCSSBaseScrollView.init();
        scrollView.frame = CGRect.init(x: 0, y: 75, width: kWidth, height: 300);
        scrollView.isPagingEnabled = true;
        scrollView.contentSize = CGSize.init(width: kWidth, height: 300);
        scrollView.showsVerticalScrollIndicator = false;
        scrollView.showsHorizontalScrollIndicator = false;
        self.addSubview(scrollView!);
        //创建tableview
        for i in 0...1 {
            let tableView = UITableView.init();
            tableView.delegate = self;
            tableView.dataSource = self;
            tableView.backgroundColor = UIColor.clear;
            tableView.showsHorizontalScrollIndicator = false;
            tableView.showsVerticalScrollIndicator = false;
            tableView.frame = CGRect.init(x: CGFloat(i)*kWidth, y: 0, width: kWidth, height: 300);
            tableView.tag = i+10;
            scrollView.addSubview(tableView);
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func requestAddressData() -> Void {
        OCSSNetRequest.requestData(url: "/StoreHouse/GetAreaChildren_app", para: ["areaId":areaId!], type: MethodType.post, success: { (result) in
            print(result);
        }) { (error) in
            
        }
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1;
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return 10;
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "ProvinceCell");
        if cell == nil {
            cell = UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "ProvinceCell")
        }
        if tableView.tag == 11 {
            cell?.textLabel?.text = "成都市";
        }else{
            cell?.textLabel?.text = "四川省";
        }
        
        return cell!;
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if tableView.tag == 10 {
            if scrollView.contentSize.width < kWidth*2{
                scrollView.contentSize = CGSize.init(width: scrollView.contentSize.width+kWidth, height: scrollView.contentSize.height);
            }
            scrollView.setContentOffset(CGPoint.init(x: kWidth*CGFloat(tableView.tag-10+1), y: 0), animated: true);
            let btn = self.viewWithTag(100) as! UIButton;
            btn.setTitle("四川省", for: UIControlState.normal);
            let nextBtn = self.viewWithTag(101) as! UIButton;
            if nextBtn.titleLabel?.text == nil {
                nextBtn.setTitle("未选择", for: UIControlState.normal);
            }
            
        }else{
            let btn = self.viewWithTag(101) as! UIButton;
            btn.setTitle("成都市", for: UIControlState.normal);
            MYBottomPopView.dismiss();
        }
    }
    
}
